<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/8/10
 * Time: 11:29
 */

namespace app\alliance\controller;

use app\service\jobService;
use app\service\MemberCode;
use app\service\RedPacketService;
use app\service\ResourceService;
use app\service\TradingService;
use Redis\Redis;
use think\App;
use think\Controller;
use think\Db;
use think\Request;

class HomeInfo extends Controller
{
    static $_Redis;
    static $token;

    function __construct(App $app = null)
    {
        parent::__construct($app);
        header('Access-Control-Allow-Origin:*');  //支持全域名访问，不安全，部署后需要固定限制为客户端网址
        header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); //支持的http 动作
        header('Access-Control-Allow-Headers:Access-Control-Allow-Headers, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Authorization , Access-Control-Request-Headers,token');  //响应头 请按照自己需求添加。
        if (strtoupper($_SERVER['REQUEST_METHOD']) == 'OPTIONS') {
            exit;
        }
        self::$_Redis = new Redis();
        $request = new Request();
        self::$token = $request->header("token");
        $jobService = new jobService;
        $jobService->timedTaskExec();
        $jobService->protectExec();
        $jobService->listenTaskExecc();
        $jobService->syncSaleBusiness();
        RedPacketService::autoRollBack();
        TradingService::unpaid();
        TradingService::autoConfirm();
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 首页弹窗
     */
    function showPop()
    {
        $status = false;
        $code = MemberCode::authCode(self::$token);
        $_list = self::$_Redis->hGetJson('showPop', "showPop");
        if (empty($_list)) {
            # 查询当前是否有弹窗
            $_pop = Db::table("notice nt")
                ->field("nt.*,concat(',',nl.member_level) member_level")
                ->join("notice_level nl", "nl.notice_id=nt.id and nt.people_type=3", "left")
                ->where(array("nt.status" => 1, "nt.type" => 3))
                ->where("date(nt.send_time)<='" . DATESTAMP . "' and nt.stop_time>='" . date("Y-m-d H:i:s") . "'")
                ->find();
            if (!empty($_pop)) {
                # 计算结束时间距离现在多少秒
                $_exec_time = strtotime($_pop['stop_time']) - time();
                $_list = array('id' => $_pop['id'], "pop_status" => true, "title" => $_pop['title'], "content" => $_pop['content'],
                    "send_time" => $_pop['send_time'], "send_name" => $_pop['send_time'], "people_type" => $_pop['people_type'], "stop_time" => $_pop['stop_time'],
                    "is_see" => $_pop['is_see'], "allow_status" => $_pop['allow_status'], "member_level" => $_pop['member_level'], "explain" => $_pop['explain']);
                self::$_Redis->hSetJson('showPop', "showPop", $_list, $_exec_time);
            } else {
                # 计算结束时间距离现在多少秒
                $_exec_time = strtotime(date("Y-m-d 23:59:59")) - time();
                self::$_Redis->hSetJson('showPop', "showPop", array("pop_status" => false), $_exec_time);
                return array("status" => $status);
            }
        }
        if ($_list['pop_status']) {
            if ($_list['send_time'] <= date("Y-m-d H:i:s") and $_list['stop_time'] >= date("Y-m-d H:i:s")) {
                $status = true;
                if ($_list['people_type'] == 2) {
                    $status = true;
                }
                if (!empty($code)) {
                    # 判断有用户信息状态
                    # 用户身份是不是合伙人
                    $memberInfo = self::$_Redis->hGetJson("memberInfo", $code);
                    $_member_type = $memberInfo['type'];
                    # 用户会员等级
                    $_member_level = $memberInfo["level"];
                    if ($_list['people_type'] == 1 and $_member_type == 1) {
                        $status = true;
                    }
                    if ($_list['people_type'] == 3 and !empty($_list['member_level'])) {
                        $_level = explode(',', $_list['member_level']);
                        if (in_array($_member_level, $_level)) {
                            $status = true;
                        }
                    }
                }
            }
        }
        return array("status" => $status, "data" => $_list);
    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 玩家活跃度
     */
    function getsActiveMember()
    {
        $_all = self::$_Redis->hGet("all_member", "all_member");
        if (empty($_all)) {
            $_basic = 2000;
            $num = Db::table("member")->count();
            $_multiple = rand(1, 10);
            $_all = $_basic + $num * $_multiple;
            self::$_Redis->hSet("all_member", "all_member", strval($_all));
        }

        $_active = self::$_Redis->hGet("active_number", "active_number");
        if (empty($_active)) {
            $_active = $_all * 0.3;
        }
        $_sign = 1;
        $_time = self::$_Redis->hGet("active_time", "active_time");
        if (empty($_time)) {
            $hour = intval(date("H"));
            if (8 <= $hour and $hour <= 22) {
                $_multiple_active = rand(1, 10);
                if ($_active < 500) {
                    $_sign = 1;
                } elseif ($_active > $_all * 0.4) {
                    $_sign = 2;
                } else {
                    $_sign = rand(1, 2);
                }
            } else {
                $_multiple_active = rand(1, 5);
                if (intval($_active) < rand(10, 100)) {
                    $_sign = 1;
                } else {
                    $_sign = 2;
                }
            }
            $_basic_active = intval($_active * $_multiple_active / 100);
            if ($_sign == 1) {
                $_active += $_basic_active;
            } else {
                $_active -= $_basic_active;
            }
            self::$_Redis->hSet("active_number", "active_number", strval($_active));
            self::$_Redis->hSet("active_time", "active_time", '1', 5);
        }
        return array("status" => true, "all_member" => float_number($_all), "all_active" => float_number(intval($_active)), "activeIcon" => $_sign);

    }

    /**
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @context 各种协议
     */
    function agreementInfo()
    {
        #类型 1 获奖规则  2. 签到规则 3.推荐规则 4.积分规则5 用户服务协议 6教程 7 玩家功略8.如何分享获得收益 9隐私政策
        $type = input('post.type');
        #规则类型 1 是规则类型  0 是其他类型  2=>协议
        $rules_type = input('post.rules_type');
        $info = Db::table('protocol')->field('content')->where(array('rules_type' => $rules_type, 'type' => $type))->find()['content'];
        $info = ResourceService::ContentStaticReplace($info);
        return array('status' => true, 'msg' => '查询成功', 'info' => $info);
    }
}